package top.lossingdawn.http.request.demo.request;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;
import top.lossingdawn.http.request.demo.entity.RequestParams;
import top.lossingdawn.http.request.demo.entity.RequestScopeDTO;

import javax.websocket.server.PathParam;
import java.io.IOException;

/**
 * @author jiangyj
 * @version 1.0
 * @create 2021-07-27 10:30
 **/
@Slf4j
@Api(tags = "GET客户端示例")
@RestController
@RequestMapping("/get-demo")
public class GetClientDemo {


    private final static String LOCALHOST = "http://127.0.0.1:18080";

    @Autowired
    private OkHttpClient okHttpClient;

    /*****path*****/
    @GetMapping("path/{id}")
    public String path(String id, String name) throws IOException {

        final Request request = new Request.Builder()
                .url(LOCALHOST + "/get-demo/path/" + id)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        return "path：" + response.body().string();
    }


    @GetMapping("path2/{id}")
    public String path2(@PathParam("id") String id, String name) {
        return "path2：" + id;
    }

    @GetMapping("path3/{id}")
    public String path3(RequestParams requestParams) {
        return "path3：" + JSON.toJSONString(requestParams);
    }


    /*****RequestParam*****/
    @GetMapping("params")
    public String params(@RequestParam String id) {
        return "params：" + id;
    }


    /*****RequestPart*****/
    @GetMapping("requestPart")
    public String requestPart(@RequestPart String id) {
        return "params：" + id;
    }


    /*****RequestHeader*****/
    @GetMapping("RequestHeader")
    public String requestHeader(@RequestHeader String id) {
        return "params：" + id;
    }


    /*****RequestAttribute*****/
    @GetMapping("RequestAttribute")
    public String requestAttribute(@RequestAttribute String id) {
        return "params：" + id;
    }

    @GetMapping("RequestScope")
    public String requestScope(RequestScopeDTO requestScopeDTO) {
        return "params：" + JSON.toJSONString(requestScopeDTO);
    }


    /*****RequestBody*****/
    @GetMapping("RequestBody")
    public String requestBody(@RequestBody String id) {
        return "params：" + id;
    }


}
